package com.hmyuan.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hmyuan.dao.CommentMapper;
import com.hmyuan.domain.Comment;
import com.hmyuan.service.CommentService;
@Service("commentService")
public class CommentServiceImpl implements CommentService {
	@Resource
	CommentMapper commentMapper;
	
	@Override
	public PageInfo<Comment> selectListByPage(Comment comment,
			int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize); // 核心分页代码
		List<Comment> list = commentMapper.selectList(comment);
		PageInfo<Comment> pageInfo = new PageInfo<Comment>(list);
		
		return pageInfo;
	}

	@Override
	public boolean insert(Comment comment) {
		// TODO Auto-generated method stub
		int num = commentMapper.insert(comment);
		return num==1?true:false;
	}

	@Override
	public Comment selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return commentMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean updateByPrimaryKeySelective(Comment comment) {
		// TODO Auto-generated method stub
		int num = commentMapper.updateByPrimaryKeySelective(comment);
		return num==1?true:false;
	}

	@Override
	public boolean deletecommentById(Long id) {
		// TODO Auto-generated method stub
		int num = commentMapper.deleteByPrimaryKey(id);
		return num==1?true:false;
	}

	@Override
	public List<Comment> selectList(Comment comment) {
		// TODO Auto-generated method stub
		return commentMapper.selectList(comment);
	}

	@Override
	public List<Comment> selectListByArticleid(Long articleid) {
		// TODO Auto-generated method stub
		if(articleid==null) return null;
		Comment comment = new Comment();
		comment.setArticleid(articleid);
		List<Comment> list = commentMapper.selectList(comment);
		List<Comment> list2 = new ArrayList<Comment>();
		for (Comment c : list) {
			if(c.getParentid()==0){
				list2.add(c);
			}else{
				for (Comment c2 : list) {
					if(c2.getId()==c.getParentid()){
						if(c2.getNotes()==null){
							c2.setNotes(new ArrayList<Comment>());
						}
						c2.getNotes().add(c);
					}
				}
			}
		}
		return list2;
	}
}
